
/**
 * 大客户项目维护
 * @author liuwei
 * @date 2019/11/20
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        table,
        vueData = {

        },
        table_head_where = {},
        table_where = {

        },
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            ,{field: "GWGF001",width:100,title:"招标编号",search:"default",searchOpts:{icon:true}}
            ,{field: "GWGA002",width:100,title:"项目名称",search:"default"}
            ,{field: "GWGF002",width:100,title:"所属区域",hide:true}
            ,{field: "GWGF002_NAME",width:100,title:"所属区域"}
            ,{field: "GWGF003",width:100,title:"开标时间",sort:true}
            ,{field: "GWGF004",width:100,title:"项目阶段",filterDatas:app.datas_('gwgf004'),
                search:"default",searchOpts:{type:'select'}}
            ,{field: "GWGF005",width:100,title:"采购预算(万元)"}
            ,{field: "GWGF006",width:100,title:"服务年限"}
            ,{field: "GWGF007",width:100,title:"入围单位",hide:true}
            ,{field: "GWGF007_NAME",width:100,title:"入围单位"}
            ,{field: "GWGF008",width:100,title:"中标单位",hide:true}
            ,{field: "GWGF008_NAME",width:100,title:"中标单位"}
            ,{field: "GWGF009",width:100,title:"中标金额"}
            ,{field: "GWGF010",width:120,title:"是否采购我司设备",filterDatas:app.datas_("sys_n_y")}
            ,{field: "GWGFACTI",width:100,title:"状态",filterDatas:app.datas_("sys_y_n")}
            ,{field: "GWGFORIU",width:100,title:"创建人",hide:true,search:"default",searchOpts:{icon:true}}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwgi003setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwgi003SettingAction,
        moduleName= "大客户项目"
    ;

    var Gwai300 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwgi003',
                _searchs: [{
                    id: 'gwgi003', tableId: 'gwgi003_table', datas: _searchsArr
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwgi003_edit', tpl: 'gwgi003_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwgi003_table'),
                    url: g_ctxPath + 'crm/gwgi003/getHeadList',
                    exportsUrl: g_ctxPath + 'crm/gwgi003/getHeadList',
                    exportsName: '客户数据',
                    title: '招标信息表',
                    method: 'post',
                    id: 'gwgi003_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwgi003setting-action').innerHTML).render({}, function (string) {
                    gwgi003SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwgi003_table)', function (obj) {

                    layer.tips(gwgi003SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwgi003_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '招标信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                    case '2':
                                        self._openTplFull({
                                            tag: 'gwgi003_edit',
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '招标信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行单击事件
                table.on('row(gwgi003_table)', function (obj) {
                    table_where.GWGA001 = obj.data.GWGA001;
                    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                });

                //监听行双击事件
                table.on('rowDouble(gwgi003_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwgi003_edit',
                        tagData: {
                            obj: obj,
                            detailReadOnly:true
                        },
                        index: 0,
                        openOpts: {
                            title: '招标信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwgi003_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwgi003_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '招标信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'valid' :
                            self.changeStatusBatch("valid");
                            break;
                        case 'invalid' :
                            self.changeStatusBatch("inValid");
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwgi003_table', where: table_head_where});
        },

        changeStatusBatch:function(action) {
            var checkStatus = table.checkStatus('gwgi003_table');
            var flag = true;
            if(checkStatus.data.length === 0) {
                app.showWarning("至少选择一笔数据进行操作");
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    let _temp = app.fields_('GWGF_T');
                    switch (action) {
                        case "valid" :  // 有效
                            if( v.GWGFACTI === "N"){
                                v.GWGFACTI = "Y"
                            }
                            else {
                                app.showWarning("无效的招标项才能进行有效操作");
                                flag = false;
                                return;
                            }
                            break;
                        case "inValid" : // 无效
                            if(v.GWGFACTI === "Y"){
                                v.GWGFACTI = "N"
                            }
                            else {
                                app.showWarning("有效的招标项才能进行无效操作");
                                flag = false;
                                return;
                            }
                            break;
                    }
                    _xdata.push({
                        GWGF001:v.GWGF001,
                        GWGFACTI:v.GWGFACTI
                    });
                });
                if(!flag){
                    return;
                }else {
                    app.ajax({
                        xurl: 'crm/gwgi003/changeStatus',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: moduleName +'状态修改成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        delData: function (obj) {
            if(obj.data.GWGFACTI !== 'N'){
                app.showWarning("只有无效的资料才能删除");
                return;
            }
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwgi003/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '招标信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwgi003_table');
            if (checkStatus.data.length > 0) {
                let _xdata = [];
                let canDelete = true;
                checkStatus.data.forEach(function (v) {
                    if(v.GWGFACTI !== 'N'){
                        canDelete = false;
                        return;
                    }
                    var _temp = app.fields_('crm_gwgi003');
                    _temp.GWGF001 = v.GWGF001;
                    _temp.GWGFENT = v.GWGFENT;
                    _xdata.push(_temp);
                });
                if(!canDelete) {
                    app.showWarning("只有无效的资料才能删除")
                }else {
                    app.ajax({
                        xurl: 'crm/gwgi003/delBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '招标信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'gwgi003',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field === 'GWGF001') {  // 招标编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwga001',
                                            columnList: [
                                                {label: '招标编号', prop: 'GWGA001', width: '150'},
                                                {label: '项目名称', prop: 'GWGA002', width: '150'},
                                                {label: '客户', prop: 'GWAKL004', width: '150'},
                                                {label: '开标时间', prop: 'GWGA006', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGF001 = data.GWGA001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择招标编号',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field === 'GWGFORIU') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'},
                                                {label: '账户', prop: 'ACCOUNT', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWGFORIU = data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择创建人',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai300();

});